<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/10/26
 * Time: 11:19
 */
namespace app\admin\controller;
use app\admin\service\PackageService;
use app\admin\service\CourseService;
use app\models\CampusPriceConfigureModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\PackageModel;

/**
 * Class Coursepackage
 * @package app\admin\controller
 */
class Package extends Common
{
    /**
     * 课程包管理
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function package_manage()
    {
        if (request()->isPost()) {
            return json((new PackageService())->package_manage());
        }
        $this->dictionary();
        return view();
    }

    /**
     * 课程
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function course()
    {
        if (request()->isPost()) {
            return json((new PackageService())->course());
        }
        $course = CourseModel::where(['status'=>1])->select();
        $this->assign('course',$course);
        return view();
    }

    /**
     * 新增课程包
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (request()->isPost()) {
            return json((new PackageService())->logic($type = 1));
        }
        $this->assign('totalprice',input('totalprice'));
        $this->assign('package',input('package'));
        $this->dictionary();
        return view();
    }

    /**
     * 修改课程包
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        if (request()->isPost()) {
            return json((new PackageService())->logic($type = 2));
        }
        $package = PackageModel::get(input('id'));
        $course_arr = explode(',',$package['course']);
        $course = '';
        foreach ($course_arr as $v){
            $course .= ','.CourseModel::where('id',$v)->value('course_name');
        }
        $arr = explode(',', $course);
        unset($arr[0]);
        $package['course'] = implode(',', $arr);
        $package['totalprice'] = $package['totalprice'] / 100;
        $package['discount'] = $package['discount'] / 100;
        $package['receivable'] = $package['receivable'] / 100;
        $this->assign('package',$package);
        $this->assign('id',input('id'));
        $this->dictionary();
        return view();
    }

    /**
     * 改变状态
     * @return \think\response\Json
     */
    public function package_check_status()
    {
        if (request()->isPost()) {
            return json((new PackageService())->package_check_status());
        }
    }
}